#include "query_database.h"

query_database::query_database(const query_database& a)
{
	this->operator =(a);
}

query_database::query_database(int n,fstream &data,fstream &states)
{
	N=n;
	overwrite_D(data);
	overwrite_SN(states);
}

query_database::query_database(matrx<int> const Data,vectr<int> States)
{
	
	if (Data.get_row()==States.get_lenght())
		N=Data.get_row();
	else
	{
		//cerr<< "contolla dim dataset NS e N";
		////system("PAUSE");
		exit(1);
	}

	NS=Data.get_column();

	
	SN=States;
	//D=Data;
	D.initialize(N,NS);
	int i,j;
	for(i=0;i<N;i++)
    {
		for( j=0;j<NS;j++)
	    {
	        D.put(i,j,Data.get(i,j));
	    }
    } 
/**/
}

const query_database& query_database::operator=(const query_database& a)
{
	if (&a != this) 
	{
		N=a.N;
		NS=a.NS;
		D=a.D;
		N=a.N;
	}
	return *this;
}

void query_database::putD(matrx<int> Data)
{
	/*D=DataSet;*/
	N=Data.get_row();
	NS=Data.get_column();
	D.initialize(N,NS);
	int i,j;
	for(i=0;i<N;i++)
    {
		for( j=0;j<NS;j++)
	    {
	        D.put(i,j,Data.get(i,j));
	    }
    }
}	


void query_database::overwrite_D(fstream &data)
{
	int c,ns,i,j,h;
	if(data.tellg())
	   NS=data.tellg()/(2*N);
	else
	{
		
			ns=0;
		while(!data.eof())
		{
			data>>c;
			ns++;
		}
		NS=ns/N;
	}
	if(NS)
	{
	    data.clear(); 
        data.seekg(0,ios::beg);
	    D.initialize(N,NS);
	    int *d,size=N*NS;
	    d=new int[size];
	    for(i=0;i<size;i++)
	        data>>d[i];
	    h=0;
	    for( j=0;j<NS;j++)
	    {
	        for(i=0;i<N;i++)
	        {
		        D.put(i,j,d[h]);
		        ++h;
	        }
        }
	    delete [] d;
	    d=NULL;
	}
	else
	{
		//cerr<<"the database is empty or number of samples insufficient\n";
		////system("PAUSE");
		exit(1);
	}
}
void query_database::overwrite_SN(fstream &states)
{
	states.clear(); 
    states.seekg(0,ios::beg);
	SN.initialize(N);
	int *d,size,h,i;
	size=N;
	d=new int[size];
	for( i=0;i<size;i++)
	    states>>d[i];
	h=0;
	for( i=0;i<N;i++)
	{
	    SN.put(i,d[h]);
	    ++h;
    }
	delete [] d;
	d=NULL;
}









     		





